<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      add: 0
    }
  },
  async mounted() {
    // 为自动化测试增加id
    this.listenDom(document.body)
  },
  async updated() {
  },
  methods: {
    getChildren2(parent) {
      const path = this.$route.path
      const nameId = path.split('/').join('')
      // 创建迭代器
      const iterator = document.createNodeIterator(parent, NodeFilter.SHOW_ELEMENT, null, false)
      // 用循环反复调用节点迭代器对象的nextNode().跳向下一个节点
      let node
      while ((node = iterator.nextNode()) != null) {
        if (!node.id && node.id.indexOf('ForTest') < 0) {
          this.add++
          node.id = nameId + this.add
        }
      }
    },
    listenDom(element) {
      const that = this
      // dom 监听函数
      var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
      var callback = function(records) {
        records.map(function(record) {
          console.log('Mutation type: ', record.type)
          console.info('Mutation target: ', record.target)
          that.getChildren2(element)
        })
      }

      var mo = new MutationObserver(callback)

      var option = {
        'childList': true,
        'subtree': true
      }

      mo.observe(element, option)
      // 停止观察
      // mo.disconnect();
    }
  }
}
</script>
